package com.ex.kotlin

/**
 * 函数<BR>
 *
 * @author W4255
 * @version [V0.0.1, 2021/8/8]
 * @Description: NA
 */
fun main() {
    val list: List<String> = listOf("apple", "zoo","pear", "banana", "watermelon")
    val result: String? = list.findMax { it.length }
    // val result = list.maxBy {fruit:String ->fruit.length}
    println(result)
}

fun <T, R : Comparable<R>> List<T>.findMax(block: (T) -> R): T? {
    if (isEmpty()) return null
    var maxEliement: T = get(0)
    var maxValue: R = block(maxEliement)
    for (element: T in this) {
        var value: R = block(element)
        if (value > maxValue) {
            maxEliement = element
            maxValue = value
        }
    }
    return maxEliement
}

fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
    return operation(num1, num2)
}